CODE 124. ZigZag Conversion

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2013/11/13/2013-11-13-CODE 124 ZigZag Conversion/

访问原文「CODE 124. ZigZag Conversion

The string "PAYPALISHIRING" is
written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R

And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public String convert(String s, int nRows) {
// Start typing your Java solution below
// DO NOT write main() function
if(nRows == 1){
return s;
}
ArrayList<ArrayList<Character>> charLists = new ArrayList<ArrayList<Character>>();
for (int i = 0; i < nRows; i++) {
charLists.add(new ArrayList<Character>());
}
char[] cs = s.toCharArray();
for (int i = 0; i < s.length(); i++) {
if (i % (nRows - 1) == 0) {
int j = 0;
for (j = i; j < cs.length && j < i + nRows; j++) {
charLists.get(j - i).add(cs[j]);
}
i = j - 1;
} else {
int row = charLists.size() - 1 - i % (nRows - 1);
charLists.get(row).add(cs[i]);
}
}
StringBuilder sb = new StringBuilder();
for (ArrayList<Character> charList : charLists) {
for (char c : charList) {
sb.append(c);
}
}
return sb.toString();
}
Jerky Lu wechat
欢迎加入微信公众号